package org.robot.scm.platform.monitor;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.robot.scm.platform.AbstractMonitor;
import org.robot.scm.pojos.BaseNotify;
import org.robot.scm.pojos.config.BaseConfig;
import org.robot.scm.pojos.enums.PlatformEnum;
import org.robot.scm.pojos.platform.OttiBaseNotify;
import org.robot.scm.utils.BaiduSpeakUtil;
import org.robot.scm.utils.DateTool;
import org.robot.scm.utils.RequestHelper;

/**
 * 可浮欧态
 */
@Slf4j
public class OttiNoticeMonitor extends AbstractMonitor {

    @Override
    public PlatformEnum getPlatform() {
        return PlatformEnum.OTTI;
    }

    @Override
    public BaseNotify doMonitor(BaseConfig config) {
        try {
            // 先获取最新设备
            Map<String, String> headers = new HashMap<>() {{
                put("applicationType", "ottai_cofoe");
                put("Authorization", "Bearer eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyVHlwZSI6MCwiZXhwIjoxNzQ4NzY4NzUwLCJ1c2VySWQiOiIyMDM0NjU2NSJ9.giGUB79mU04CgXrUegu2kkUzP2T1CykD0Wt7c7oeQFbz_sB7rBtr4OiyBBAlOOqjSAWhN9Ejgg92Srg22f-d8g");
                put("Content-Type", "application/json");
                put("userId", "20346565");
                put("appName", "miniprogram");
                put("timezone", "28800");
                put("xweb_xhr", "1");
            }};
            String result = RequestHelper.get(config.getCurrentPlatform().getNoticeSrc(), headers);
            JSONObject jsonObject = JSON.parseObject(result);
            if (!StringUtils.equals(jsonObject.getString("code"), "OK")) {
                return null;
            }

            JSONObject glucoseData = jsonObject.getJSONObject("data");
            if (Objects.isNull(glucoseData) || glucoseData.isEmpty()) {
                return null;
            }

            return glucoseData.toJavaObject(OttiBaseNotify.class);
        } catch (Exception e) {
            log.warn("otti error: {}", e.getMessage());
            return null;
        }
    }

    @Override
    public void doAfter(BaseConfig config, BaseNotify notice) {
        OttiBaseNotify thisNotice = (OttiBaseNotify) notice;
        //夜间低血糖提醒并播报
        if (config.get().getNightMode().isNight(config) && thisNotice.isLowGlucose(config)) {
            speak(config, thisNotice);
            return;
        }
        //白天血糖异常提醒并播报
        if (!config.get().getNightMode().isNight(config) && thisNotice.isAbnormal(config)) {
            speak(config, thisNotice);
        }
    }

    private void speak(BaseConfig config, OttiBaseNotify thisNotice) {
        if (!config.getCurrentPlatform().isPlaySound()) {
            return;
        }
        String nowDate = DateTool.format(new Date(), DateTool.PATTERN_4);
        StringBuilder text = new StringBuilder("现在时间是 " + nowDate + ", 您的血糖值为:  " + thisNotice.getGlucose());
        if (thisNotice.getGlucose() < 3.9) {
            text.append(" ,血糖有点低, 请注意防范低血糖风险");
        }
        if (thisNotice.getGlucose() > 10) {
            text.append(" ,血糖非常高, 请注意控制您的饮食");
        }
        BaiduSpeakUtil.speak(text.toString());
    }
}
